package com.maotai.bl.thread;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import cn.hellotrue.HelloTrueHY;
import cn.rk.RuoKuai;

import com.alibaba.fastjson.JSONObject;
import com.maotai.bl.bean.BLWebCookie;
import com.maotai.bl.service.BLService;
import com.maotai.bl.util.BLHttpClientUtil;
import com.maotai.bl.util.BLJSUtil;

public class BLRegisterThreadHY extends Thread {

	private BLService blService;
	private String token;


	public BLRegisterThreadHY(BLService blService, String token) {
		super();
		this.blService = blService;
		this.token = token;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}


	public BLService getBlService() {
		return blService;
	}

	public void setBlService(BLService blService) {
		this.blService = blService;
	}

	@Override
	public void run() {
		HelloTrueHY ht = null;
		String telphone = null;
		try {
			BLWebCookie mwc = new BLWebCookie();
			BLHttpClientUtil hcu = BLHttpClientUtil.getInstance();
//			JSONArray jArray = IPFactory.getIpAndPort();
//			JSONObject jj = (JSONObject) jArray.get(0);
//			String ipAndPort = jj.getString("ip") + ":"
//					+ jj.getString("port");
			//现货区号码
			ht = new HelloTrueHY();
			telphone = ht.getPhone_BL(token);
			System.out.println("获取到的号码为："+telphone);
			if(telphone.contains("htm")){
				return;
			}
//			RequestConfig requestConfig = IPFactory.getKdRequestConfig(ipAndPort);
			String imageUrl = "https://passport.bl.com/sidebar/verifyCode.html?v="+System.currentTimeMillis();
			String referer = "https://reg.bl.com/regist.html";
			byte[] retbyte = hcu.getImage(imageUrl,mwc,referer);
			//1.获取图片验证码，进行打码
			String temp =RuoKuai.createByPost("vipwangtao888","WT985200","3040","95000","102790","3bbcf57ca4fb44f8b6134dd2c317de24",retbyte);
			//{"Result":"fpxr","Id":"27a3809a-49e2-496e-bb1e-74489ad586bc"}
			String verifyCode = "";
			if(temp.contains("Result")){
				JSONObject reJ = JSONObject.parseObject(temp);
				verifyCode = reJ.getString("Result");
			}else{
				return;
			}
			//2.发送短信验证码
			String sendRegurl = "https://reg.bl.com/nj/sendRegistShortMsgCode.html";
			Map<String,String> sendMap = new HashMap<String,String>();
			sendMap.put("mobile", telphone);
			sendMap.put("verifyCode", verifyCode);
			//mobile=13372007847&verifyCode=0u00
			mwc = hcu.sendHttpPostProxy(sendRegurl,mwc,sendMap,null,referer);
			System.out.println(mwc.getContent());
			if(mwc.getContent().contains("错误")){
				return;
			}
			//3.重新获取图片验证码，提交注册信息
			//TODO 获取验证码
			//loginId=nickboowen&mobile=13372007847&verifyCode=wwt3&smsCode=123456&email=nickboowen%40126.com&userAgreement=ok&adChannel=&adId=&orgId=&shopId=&putonNum=&password=
			String message = ht.getMessage_BL(token, telphone);
			System.out.println("获取到的验证码信息为：" + message);
			if("超时".equals(message)){
				try {
					throw new Exception("未获取到验证码");
				} catch (Exception e) {
				}
			}
			String regEx="[^0-9]";   
			Pattern p = Pattern.compile(regEx);   
			String telCode = p.matcher(message).replaceAll("").trim().substring(0,6);
			System.out.println("截取后的验证码信息为：" + telCode);
			
			//重新获取验证码
			String imageUrl2 = "https://passport.bl.com/sidebar/verifyCode.html?v="+System.currentTimeMillis();
			byte[] retbyte2 = hcu.getImage(imageUrl2,mwc,referer);
			String temp2 =RuoKuai.createByPost("vipwangtao888","WT985200","3040","95000","102790","3bbcf57ca4fb44f8b6134dd2c317de24",retbyte2);
			//{"Result":"fpxr","Id":"27a3809a-49e2-496e-bb1e-74489ad586bc"}
			String verifyCode2 = "";
			if(temp2.contains("Result")){
				JSONObject reJ2 = JSONObject.parseObject(temp2);
				verifyCode2 = reJ2.getString("Result");
			}else{
				return;
			}
			/**
			 * loginId: nickboowen
				mobile: 13372007847
				verifyCode: vpqt
				smsCode: 123454
				email: nickboowen@126.com
				userAgreement: ok
				adChannel: 
				adId: 
				orgId: 
				shopId: 
				putonNum: 
				password: SIVB1eblm BD00YRTcFBG6hiFUGqZyg3DSpUNFkj0DshlCfgNPEMxIhjN1UeV7Cv9f17R246IScrafQnyxOEkEgcOHlU2ceSAAxrY/hAoVJtwUn44xSKwwnNIOAHf/bDVTX HM2KxDsy3KQI14OlIxf2HGhpAn0m977CIfSwpt0ChBc5CcQuboXrtfAJGo41/1/Fk6HerAKtQ4CJ3F0E9UXLB9cLfKa4jrfoLHnvDc5dzJ3zsuY9JYQScIw1iSmTMGjEyRJ/8b0MIDE8zypG 7Znbb84JDfdGsvplzUUlRKe7l6OQtVOlZOX9wuiiQV9VIbamNYcoonN98CdSwylgw==

			 */
			String regesterUrl = "https://reg.bl.com/nj/doregist.html";
			Map<String,String> registerMap = new HashMap<String,String>();
			registerMap.put("loginId", "ab"+telphone);
			registerMap.put("mobile", telphone);
			registerMap.put("verifyCode", verifyCode2);
			registerMap.put("smsCode", telCode);
			registerMap.put("email", telphone+"@126.com");
			registerMap.put("userAgreement", "ok");
			registerMap.put("adChannel", "");
			registerMap.put("orgId", "");
			registerMap.put("shopId", "");
			registerMap.put("putonNum", "");
			String getPubKeyUrl = "https://reg.bl.com/nj/getPublicKey.html";
			mwc = hcu.sendHttpPostProxy(getPubKeyUrl, mwc, null, null, referer);
			JSONObject jsObj = JSONObject.parseObject(mwc.getContent());
			String exponent = jsObj.getString("exponent");
			String modulus = jsObj.getString("modulus");
			//mwc中有获取到的加密键值对
			String password = BLJSUtil.excuFunctionA("ab"+telphone, modulus, exponent);
			registerMap.put("password", password);
			mwc = hcu.sendHttpPostProxy(regesterUrl,mwc,registerMap,null,referer);
			System.out.println(mwc.getContent());
			//此账号已存在
			if(mwc.getContent().contains("此账号已存在")){
				return;
			}
			//重新获取验证码
			String imageUrl3 = "https://passport.bl.com/sidebar/verifyCode.html?v="+System.currentTimeMillis();
			byte[] retbyte3 = hcu.getImage(imageUrl3,mwc,referer);
			String temp3 =RuoKuai.createByPost("vipwangtao888","WT985200","3040","95000","102790","3bbcf57ca4fb44f8b6134dd2c317de24",retbyte3);
			//{"Result":"fpxr","Id":"27a3809a-49e2-496e-bb1e-74489ad586bc"}
			String verifyCode3 = "";
			if(temp3.contains("Result")){
				JSONObject reJ3 = JSONObject.parseObject(temp3);
				verifyCode3 = reJ3.getString("Result");
			}else{
				return;
			}
			mwc = hcu.sendHttpPostProxy(getPubKeyUrl, mwc, null, null, referer);
			jsObj = JSONObject.parseObject(mwc.getContent());
			exponent = jsObj.getString("exponent");
			modulus = jsObj.getString("modulus");
			//mwc中有获取到的加密键值对
			password = BLJSUtil.excuFunctionA("ab"+telphone, modulus, exponent);
			
			//增加实名操作 1.先登录，后发送短信到手机验证，最后提交实名信息
			String loginUrl = "https://passport.bl.com/nj/dologin.html";
			Map<String,String> loginMap = new HashMap<String,String>();
			loginMap.put("loginId", "ab"+telphone);
			loginMap.put("returnurl", "");
			loginMap.put("verifyCode", verifyCode3);
			loginMap.put("password", password);
			mwc = hcu.sendHttpPostProxy(loginUrl,mwc,loginMap,null,referer);
			//开始实名
//			String phoneCodeUrl = "https://my.bl.com/ym/nl/nj/getPhoneCode.html";
//			mwc = hcu.sendHttpPostProxy(phoneCodeUrl,mwc,null,null,"https://my.bl.com/ym/nl/toNameVerification.html");
			//保存实名
			String saveNameUrl = "https://my.bl.com/ym/nl/nj/saveNameVerification.html";
			//name:冯亚兰
			//certType:01
			//certCode:320981199801187462
			//从数据库中随机取一个身份证
			for(int i=0;i<10;i++){
				Map<String,Object> shenfenMap = blService.getShenFengRandom();
				System.out.println(shenfenMap);
				Map<String,String> saveMap = new HashMap<String,String>();
				saveMap.put("name", shenfenMap.get("name")+"");
				saveMap.put("certType", "01");
				saveMap.put("certCode", shenfenMap.get("shenfenzheng")+"");
				mwc = hcu.sendHttpPostProxy(saveNameUrl,mwc,saveMap,null,"https://my.bl.com/ym/nl/toNameVerification.html");
				System.out.println(mwc.getContent());
				if(mwc.getContent().contains("不正确")){
					continue;
				}else{
					//如果成功，那么插入数据库
					Map<String,String> inserMap = new HashMap<String,String>();
					inserMap.put("code", telphone);
					inserMap.put("pwd", "ab"+telphone);
					blService.insertZhanghaoWeb(inserMap);
					break;
				}
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
}
